package com.sali.service.data_file_uploader.refcount;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.List;

/**
 * @author sali
 * @date 2025/3/16 23:11
 */
public class ReferenceCountMonitor implements Runnable {

    public static Logger log = LoggerFactory.getLogger(Runnable.class);

    @Override
    public void run() {
        List<ReferenceCounter> processed = ReferenceManager.hasProcessed();
        for (ReferenceCounter counter : processed) {
            //1、移除改zip的引用计数
            ReferenceManager.removeRefCounter(counter.getSourceFileId());
            if (log.isDebugEnabled()) {
                log.debug("[引用计数] 清理{}的引用", counter.getSourceFilePath());
            }

            // 删除源文件
            File file = new File(counter.getSourceFilePath());
            if (file.exists()) {
                if (file.delete()) {
                    log.info("文件删除成功: {}", counter.getSourceFilePath());
                } else {
                    log.warn("文件删除失败: {}", counter.getSourceFilePath());
                }
            } else {
                log.warn("文件不存在: {}", counter.getSourceFilePath());
            }
        }
    }
}
